package com.ycl.plus.lm.hardware.service.impl;

import com.ycl.plus.lm.common.core.utils.MapstructUtils;
import com.ycl.plus.lm.common.core.utils.StringUtils;
import com.ycl.plus.lm.common.mybatis.core.page.TableDataInfo;
import com.ycl.plus.lm.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ycl.plus.lm.hardware.domain.bo.HardwareEquipSensorBo;
import com.ycl.plus.lm.hardware.domain.vo.HardwareEquipSensorVo;
import com.ycl.plus.lm.hardware.domain.HardwareEquipSensor;
import com.ycl.plus.lm.hardware.mapper.HardwareEquipSensorMapper;
import com.ycl.plus.lm.hardware.service.IHardwareEquipSensorService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备和传感器关联Service业务层处理
 *
 * @author devil-ycl
 * @date 2024-11-07
 */
@RequiredArgsConstructor
@Service
public class HardwareEquipSensorServiceImpl implements IHardwareEquipSensorService {
    @SuppressWarnings("all")
    private final HardwareEquipSensorMapper baseMapper;

    /**
     * 查询设备和传感器关联
     *
     * @param equipId 主键
     * @return 设备和传感器关联
     */
    @Override
    public HardwareEquipSensorVo queryById(Long equipId){
        return baseMapper.selectVoById(equipId);
    }

    /**
     * 分页查询设备和传感器关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设备和传感器关联分页列表
     */
    @Override
    public TableDataInfo<HardwareEquipSensorVo> queryPageList(HardwareEquipSensorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HardwareEquipSensor> lqw = buildQueryWrapper(bo);
        Page<HardwareEquipSensorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的设备和传感器关联列表
     *
     * @param bo 查询条件
     * @return 设备和传感器关联列表
     */
    @Override
    public List<HardwareEquipSensorVo> queryList(HardwareEquipSensorBo bo) {
        LambdaQueryWrapper<HardwareEquipSensor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HardwareEquipSensor> buildQueryWrapper(HardwareEquipSensorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HardwareEquipSensor> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增设备和传感器关联
     *
     * @param bo 设备和传感器关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HardwareEquipSensorBo bo) {
        HardwareEquipSensor add = MapstructUtils.convert(bo, HardwareEquipSensor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEquipId(add.getEquipId());
        }
        return flag;
    }

    /**
     * 修改设备和传感器关联
     *
     * @param bo 设备和传感器关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HardwareEquipSensorBo bo) {
        HardwareEquipSensor update = MapstructUtils.convert(bo, HardwareEquipSensor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HardwareEquipSensor entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除设备和传感器关联信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
